import 'package:flutter/foundation.dart';
import 'package:share_fit/app/controllers/base/base_controller.dart';
import 'package:share_fit/app/controllers/device/hr/hr_const.dart';
import 'package:share_fit/app/tools/log_utils.dart';
import 'package:share_fit/app/ute/uteped_const.dart';
import 'package:share_fit/app/ute/uteped_device.dart';

import 'hr_warning_state.dart';

class HrWarningLogic extends BaseController {
  final HrWarningState state = HrWarningState();

  void setContinuousHeartRateEnable(value) {
    showLoading();
    state.isContinuousHeartRate.value = value;
    bRemoteReqAsync(HrConst.setContinuousHeartRate, {UTEPedConst.keyEnable: value})
        .then((success) => _onSetContinuousHeartRateEnable(success, value));
  }

  void setMaxHeartRate(value) {
    state.maxHeartRate.value = value;
    save(HrConst.maxHeartRate, value);
    bRemoteReqAsync(HrConst.setHighestRemind, {UTEPedConst.keyEnable: true, HrConst.keyLimit: value})
        .then((success) => {
          if (success) {
            save(HrConst.maxHeartRate, value)
          }
    });
  }

  void setMinHeartRate(value) {
    state.minHeartRate.value = value;
    save(HrConst.minHeartRate, value);
    bRemoteReqAsync(HrConst.setLowestRemind, {UTEPedConst.keyEnable: true, HrConst.keyLimit: value})
        .then((success) => {
          if (success) {
            save(HrConst.minHeartRate, value)
          }
    });
  }

  @override
  String getRemotePath() {
    return UTEPedConst.pathDeviceOptions;
  }

  @override
  String getStoragePath() {
    return UTEPedConst.pathDevice;
  }

  @override
  void initState() {
    // empty
    state.isContinuousHeartRate.value = read(HrConst.isContinuousHeartRate, defaultValue: false);
    state.minHeartRate.value = read(HrConst.minHeartRate, defaultValue: 60);
    state.maxHeartRate.value = read(HrConst.maxHeartRate, defaultValue: 100);
  }

  @override
  void onRemoteEvent(event, method) {
    // empty
  }

  void _onSetContinuousHeartRateEnable(success, value) {
    cancelLoading();
    if (success) {
      save(HrConst.isContinuousHeartRate, value);
    }
    if (kDebugMode) {
      UTEPedDevice.instance.getDefaultConfiguration().then((value) => logger.d(value));
    }
  }
}
